package com.zzyy.dtx.order.service.impl;

import com.zzyy.dtx.coupon.api.CouponApi;
import com.zzyy.dtx.coupon.dto.CouponDTO;
import com.zzyy.dtx.inventory.api.InventoryApi;
import com.zzyy.dtx.inventory.dto.InventoryDTO;
import com.zzyy.dtx.order.dto.OrderDTO;
import com.zzyy.dtx.order.entities.Order;
import com.zzyy.dtx.order.enums.OrderStatusEnum;
import com.zzyy.dtx.order.mapper.OrderMapper;
import com.zzyy.dtx.order.service.OrderService;
import com.zzyy.dtx.score.api.ScoreApi;
import com.zzyy.dtx.score.dto.ScoreDTO;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.annotation.Hmily;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @auther zzyy
 * @create 2021-04-17 22:28
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService
{
    @Resource
    private OrderMapper orderMapper;
    @Resource//订单微服务通过OpenFeign去调用库存微服务
    private InventoryApi inventoryApi;
    @Resource//订单微服务通过OpenFeign去调用积分微服务
    private ScoreApi scoreApi;
    @Resource//订单微服务通过OpenFeign去调用优惠券微服务
    private CouponApi couponApi;;

    @Override
    @Hmily(confirmMethod = "confirmMethod",cancelMethod = "cancelMethod")
    @Transactional
    public int add(Order order)
    {
        log.info("--------进入订单try");
        order.setStatus(OrderStatusEnum.CONFIRM.getCode());
        int result = orderMapper.insertSelective(order);

        if(result > 0)
        {
            //减库存
            InventoryDTO inventoryDTO=new InventoryDTO();

            inventoryDTO.setUserId(order.getUserId());
            inventoryDTO.setProductId(order.getProductId());
            inventoryDTO.setCount(order.getProductCount());
            inventoryDTO.setOrderNo(order.getOrderNo());

            String decrease = "test wait";
            decrease = inventoryApi.decrease(inventoryDTO);

            //减积分
            ScoreDTO scoreDTO=new ScoreDTO();
            scoreDTO.setUserId(order.getUserId());
            scoreDTO.setOrderNo(order.getOrderNo());
            scoreDTO.setScore(order.getDecreaseScore());
            this.scoreApi.decrease(scoreDTO);

            //扣优惠卷
            CouponDTO couponDTO=new CouponDTO();
            couponDTO.setUserId(order.getUserId());
            couponDTO.setOrderNo(order.getOrderNo());
            couponDTO.setCouponId(order.getCouponReceiveId());
            this.couponApi.decrease(couponDTO);
        }
        //int i = 10/0;// try出错没加@Transactional,直接走cancelscoreApi
        //int i = 10/0;// try出错加@Transactional，订单无法进去tx_order表，被回滚了+直接走cancel

        return result;
    }

    public int confirmMethod(Order order)
    {
        log.info("--------进入订单confirm");

        //严谨写法，先查一次
        Order orderCondition = new Order();
        orderCondition.setOrderNo(order.getOrderNo());
        Order orderUpdateEntity = orderMapper.selectOne(orderCondition);
        if (orderUpdateEntity != null) {
            //按照订单号查出来的订单entity再修改状态为：未支付
            orderUpdateEntity.setStatus(OrderStatusEnum.NOT_PAY.getCode());//NOT_PAY((byte)2, "未支付"),
            //再更新
            orderMapper.updateByPrimaryKeySelective(orderUpdateEntity);
        }else{
            System.out.println("--------------订单confirm没有对应记录: "+order.getOrderNo());
            return -1;
        }
        //int i=10/0; // confirmMethod出错没加@Transactional,会在TCC2号库hmily_dtx_order8001表，留下记录，修复bug重试
        return 1;
    }

    public int cancelMethod(Order order)
    {
        log.info("--------进入订单cancel");

        //严谨写法，先查一次
        Order orderCondition = new Order();
        orderCondition.setOrderNo(order.getOrderNo());
        Order orderUpdateEntity = orderMapper.selectOne(orderCondition);
        if (orderUpdateEntity != null) {
            //按照订单号查出来的订单entity再修改状态为：拍下失败
            orderUpdateEntity.setStatus(OrderStatusEnum.CONFIRM_FAIL.getCode());// CONFIRM_FAIL((byte)1, "拍下失败"),
            //再更新
            orderMapper.updateByPrimaryKeySelective(orderUpdateEntity);
        }else{
            System.out.println("--------------订单cancel没有对应记录: "+order.getOrderNo());
            return -1;
        }
        return 1;
    }

    //=============================2023年新开始
    /**
     * 2023.4.16，查找所有的订单信息,提供给PayController类里面的getOrderList方法使用
     * @return
     */
    public List<Order> findAll(){
        List<Order> orderList=this.orderMapper.selectAll();
        return orderList;
    }

    public Order findOrderNo(String orderNo){
        Order order=new Order();
        order.setOrderNo(orderNo);
        order=this.orderMapper.selectOne(order);

        return order;
    }
}

